package com.yk.i_wms.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yk.i_wms.common.model.Result;
import com.yk.i_wms.mapper.PdtFeederDetailMapper;
import com.yk.i_wms.model.PdtFeeder;
import com.yk.i_wms.model.PdtFeederDetail;
import com.yk.i_wms.service.IPdtFeederDetailService;
import com.yk.i_wms.service.IPdtFeederService;
import com.yk.i_wms.util.EasyPoiExcelUtil;
import com.yk.i_wms.vo.PdtFeederDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 料站表详情
 *
 * @author hbq
 * @date 2021-05-12 13:52:57
 */
@Slf4j
@Service
public class PdtFeederDetailServiceImpl extends ServiceImpl<PdtFeederDetailMapper, PdtFeederDetail> implements IPdtFeederDetailService {
    @Resource
    private PdtFeederDetailMapper pdtFeederDetailMapper;
    @Autowired
    private IPdtFeederService pdtFeederService;
    /**
     * 列表
     * @param params
     * @return
     */
    public Page<Map> findList(Map<String, Object> params){
        Integer pageNum = MapUtils.getInteger(params, "pageNum");
        Integer pageSize = MapUtils.getInteger(params, "pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = -1;
        }
        Page<Map> pages = new Page<>(pageNum, pageSize);
        return pdtFeederDetailMapper.findList(pages, params);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result leadIn(MultipartFile excel, String createName) throws Exception {
        try{
            int rowNum = 0;
            if (!excel.isEmpty()) {
                ExcelReader reader = ExcelUtil.getReader(excel.getInputStream());
                List<List<Object>> read = reader.read(0, 4);
                PdtFeeder pdtFeeder = new PdtFeeder();
                pdtFeeder.setFeederName(Convert.toStr(read.get(0).get(1)));
                pdtFeeder.setModelCode(Convert.toStr(read.get(1).get(1)));
                pdtFeeder.setLine(Convert.toStr(read.get(2).get(1)));
                String stringCellValue = Convert.toStr(read.get(3).get(1));
                String scFlag="";
                if ("正面".equals(stringCellValue)){
                    scFlag="S";
                }
                if ("反面".equals(stringCellValue)){
                    scFlag="C";
                }
                if ("单面".equals(stringCellValue)){
                    scFlag="A";
                }
                pdtFeeder.setScFlag(scFlag);
                pdtFeeder.setVer(Convert.toStr(read.get(4).get(1)));
                pdtFeederService.save(pdtFeeder);

                List<PdtFeederDetail> list = EasyPoiExcelUtil.importExcel(excel, 6, 1, PdtFeederDetail.class);
                rowNum = list.size();
                if (rowNum > 0) {
                    list.forEach(u -> {
                        u.setFeederId(pdtFeeder.getId());
                        u.setCreateName(createName);
                        save(u);
                    });
                    return Result.succeed("成功导入信息"+rowNum+"行数据");
                }
            }
           return Result.failed("导入失败");
        }
        catch (Exception e){
            // 事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.failed( e,"保存失败,请检查导入模板");
        }
    }

    @Override
    public List<PdtFeederDetailVO> getFeederDetailList(List<Long> ids) {
        return pdtFeederDetailMapper.getFeederDetailList(ids);
    }
}
